<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \think\Model
 */
class AdminProblemType extends Model
{
    //
    const TYPE_ARR = [1 => '问题类型', 2 => '反馈类型'];
    const CACHE_KEY = 'problem-type-';
    protected $autoWriteTimestamp = true;

    public function getTypeTextAttr($value, $data)
    {
        return self::TYPE_ARR[$data['type']];
    }

    public static function onAfterWrite($model)
    {
        $type = $model->getAttr('type');
        cache(self::CACHE_KEY.$type, null);
    }

    public static function getTypeList($type)
    {
        $key = self::CACHE_KEY.$type;
        $res = cache($key);
        if ($res) {
            return $res;
        }
        $list = (new static())->where('is_delete', 0)
            ->where('type', $type)
            ->field('title,id')
            ->select()
            ->toArray();
        cache($key, $list);
        return $list;
    }
}
